package com.fr.report.write;

import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.core.DataXMLUtils;
import com.fr.data.core.db.DBUtils;
import com.fr.data.impl.NameDatabaseConnection;
import com.fr.report.Report;
import com.fr.report.TableDataSource;
import com.fr.report.core.FormException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Map;

public class FreeSQLSubmiter extends AbstractSubmiter
{
  private String dbName;
  private String sql;

  public String getDBName()
  {
    return this.dbName;
  }

  public void setDBName(String paramString)
  {
    this.dbName = paramString;
  }

  public String getSQL()
  {
    return this.sql;
  }

  public void setSQL(String paramString)
  {
    this.sql = paramString;
  }

  public void execute(Report paramReport, TableDataSource paramTableDataSource, Map paramMap)
    throws Exception
  {
    try
    {
      super.execute(paramReport, paramTableDataSource, paramMap);
    }
    catch (MismatchException localMismatchException)
    {
      return;
    }
    if ((this.dbName == null) || (this.sql == null))
      return;
    NameDatabaseConnection localNameDatabaseConnection = new NameDatabaseConnection(this.dbName);
    Connection localConnection = null;
    try
    {
      localConnection = localNameDatabaseConnection.createConnection();
    }
    catch (Exception localException1)
    {
      if (localConnection != null)
        DBUtils.rollback(localConnection);
      throw new FormException("can't connect to Database: " + this.dbName + "\n" + localException1.getMessage(), localException1);
    }
    Statement localStatement = null;
    try
    {
      localStatement = localConnection.createStatement();
      localStatement.execute(this.sql);
      return;
    }
    catch (Exception localException2)
    {
      throw new FormException("submiter error happens.\n" + localException2.getMessage(), localException2);
    }
    finally
    {
      if (localConnection != null)
        DBUtils.closeConnection(localConnection);
    }
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str2;
      String str1 = paramXMLableReader.getTagName();
      if ("DBName".equals(str1))
      {
        if ((str2 = paramXMLableReader.getElementValue()) != null)
          setDBName(str2);
      }
      else if ("SQL".equals(str1))
      {
        if ((str2 = paramXMLableReader.getElementValue()) != null)
          setSQL(str2);
      }
      else if ("Database".equals(str1))
      {
        NameDatabaseConnection localNameDatabaseConnection = (NameDatabaseConnection)DataXMLUtils.readXMLConnection(paramXMLableReader);
        setDBName(localNameDatabaseConnection.getName());
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("DBName").textNode(getDBName()).end().startTAG("SQL").textNode(getSQL()).end();
    super.writeXML(paramXMLPrintWriter);
  }
}